<?php
/*------------------------------------------------------------------------
# com_vportfolio - Vportfolio component
# ------------------------------------------------------------------------
# author    Vsmart Extensions
# copyright Copyright (C) 2010 www.vsmart-extensions.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.vsmart-extensions.com
# Technical Support:  Forum - http://www.vsmart-extensions.com
-------------------------------------------------------------------------*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

/**
 * @package		Joomla
 * @subpackage	Vportfolio
 */
class PortfolioModelPortfolio extends JModel
{
	/**
	 * Builds the query to select Portfolio items
	 * @param array
	 * @return string
	 * @access protected
	 */
	function _getPortfolioQuery( &$options )
	{
		// TODO: Cache on the fingerprint of the arguments
		$db			=& JFactory::getDBO();
		$id			= @$options['id'];

		$select = 'i.*, c.name ';
		$from	= '#__vportfolio AS i';

		$joins[] = 'INNER JOIN #__vportfolio_category AS c on c.id = i.categoryId ';

		$wheres[] = 'i.id = ' . (int) $id;
		$wheres[] = 'i.public = 1';
		$wheres[] = 'c.public = 1';

		/*
		 * Query to retrieve all categories that belong under the Portfolio
		 * section and that are published.
		 */
		$query = 'SELECT ' . $select .
				' FROM ' . $from .
				' '. implode ( ' ', $joins ) .
				' WHERE ' . implode( ' AND ', $wheres );

		return $query;
	}

	/**
	 * Gets a list of categories
	 * @param array
	 * @return mixed Object or null
	 */
	function getPortfolio( $options=array() )
	{
		
		$query	= $this->_getPortfolioQuery( $options );
		$result = $this->_getList( $query );
		$result[0]->images = $this->getImages($result[0]->id);
		$result[0]->reviews = $this->getReviews($result[0]->id);
		$result[0]->rating = $this->getRating($result[0]->id);
		$result[0]->users = $this->getUsers($result[0]->developer_id);
		return @$result[0];
	}
	/**
	 * Gets a list of categories
	 * @param array
	 * @return mixed Object or null
	 */
	function getNextPortfolio( $options=array() )
	{
		$db			=& JFactory::getDBO();
		$id			= @$options['id'];
		$query = ' SELECT id, title '.
				 ' FROM #__vportfolio '.			
				 ' WHERE id > '.$id.' AND public=1 ORDER BY id ASC LIMIT 1 ';
		$db->setQuery($query);
		$next = $db->loadObject();
		if(count($next) > 0)
			return $next;
		else
			return 0;
	}
	/**
	 * Gets a list of categories
	 * @param array
	 * @return mixed Object or null
	 */
	function getPrevPortfolio( $options=array() )
	{
		
		$db			=& JFactory::getDBO();
		$id			= @$options['id'];
		$query = 'SELECT id, title '.
				' FROM #__vportfolio '.			
				' WHERE id < '.$id.' AND public=1 ORDER BY id DESC LIMIT 1 ';		
		$db->setQuery($query);
		$prev = $db->loadObject();
		if(count($prev) > 0)
			return $prev;
		else
			return 0;	
	}
	
	function getImages( $id )
	{
		$db 		=& JFactory::getDBO();
		
		$query = 'SELECT * FROM #__vportfolio_image WHERE itemId="'.$id.'" and `default` <> 1 ORDER BY `default` DESC, id ASC ';
		$db->setQuery($query);
		$images = $db->loadObjectList();
		if(count($images) > 0)
			return $images;
		else
			return 0;
	}
	function getReviews( $id )
	{
		$db 		=& JFactory::getDBO();
		
		$query = 'SELECT * FROM #__vportfolio_review WHERE itemId="'.$id.'" and `public`="1"';
		$db->setQuery($query); 
		$reviews = $db->loadObjectList();
		if(count($reviews) > 0)
			return $reviews;
		else
			return 0;
	}
	function updateHit( &$options )
	{
		// TODO: Cache on the fingerprint of the arguments
		$db			=& JFactory::getDBO();
		$id			= @$options['id'];

		$sql = "UPDATE #__vportfolio SET hit = (hit + 1) WHERE id = '".$id."'";
		$db->setQuery($sql);
		$db->query();
	}
	function getRating( $id )
	{
		$db 		=& JFactory::getDBO();
		
		$query = 'SELECT * FROM #__vportfolio_rating WHERE itemId="'.$id.'"';
		$db->setQuery($query); 
		$ratings = $db->loadObjectList();
		
		if(count($ratings) <= 0 )
			$rating = 0;
		else{
			$sum = 0;
			$num = count($ratings);
			for($i=0;$i<sizeof($ratings);$i++){
				$sum = $sum + $ratings[$i]->rating;
			}
			$rating = $sum/$num;
			$rating = round($rating);
		}
		
		return $rating;
	}
	function getUsers($userIds){
		$db 		=& JFactory::getDBO();
		if($userIds !=""){
		
			$query = 'SELECT id,name FROM #__users WHERE id in ('.$userIds.')';
			$db->setQuery($query); 
			$users = $db->loadObjectList();
			return $users;
			
		}else{
			return 0;
		}
	}
	
}